import common.CommonFunction;
import common.Mask;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;



[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;

public var infoObject:Object;

public function onCreationComplete():void
{
	infoObject = new Object();
	infoObject["date"] = new Date();	
	
	getAreaCode.send(infoObject);
	
	Mask.show(CommonFunction.getMessages('pleaseWait'));
}

public var customerXML:XML;
public var customerList:ArrayCollection;
public function onSuccess(event:ResultEvent):void
{
	Mask.close();
	customerXML = event.result as XML;
		
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, code:obj.code, value:obj.value});
	}
	areacode.dataProvider = customerList;
	areacode.labelField = "value";
}

public function onSuccesss(event:ResultEvent):void
{
	customerXML = event.result as XML;
	
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, code:obj.code});
	}
	extencode.dataProvider = customerList;
	extencode.labelField = "code";
}

public function onFailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(event.message + "");
}

protected function dis_clickHandler(event:MouseEvent):void
{
	// TODO Auto-generated method stub
	infoObject["date"] = new Date();
	infoObject["areacode"] = areacode.selectedLabel;
	infoObject["extencode"] = extencode.selectedLabel;
	var str:String = ip1.text + "." + ip2.text + "." + ip3.text + "." + ip4.text ;
	infoObject["ip"] = str;
	insertData.send(infoObject);
}

public function insertSuccess(event:ResultEvent):void
{
	customerXML = event.result as XML;
	if(customerXML.booleanvalue == "true")
	{
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
		infoObject = new Object();
		infoObject["date"] = new Date();
		infoObject["areacode"] = areacode.selectedLabel;
		getData.send(infoObject);
		getExtensionNumber.send(infoObject);
		extencode.selectedIndex = -1;
		ip1.text = ip2.text = ip3.text = ip4.text = "";
	}
	else
	{
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
}

import mx.events.ListEvent;
public var getDataExt:XML;
protected function areacode_changeHandler(event:ListEvent):void
{
	extencode.selectedIndex = -1;
	ip1.text = ip2.text = ip3.text = ip4.text = "";
	infoObject["date"] = new Date();
	infoObject["areacode"] = areacode.selectedLabel;
	getData.send(infoObject);
	getExtensionNumber.send(infoObject);
}

public function getSuccess(event:ResultEvent) :void
{
	getDataExt = event.result as XML;
	customerList = new ArrayCollection();
	for each(var obj:Object in getDataExt.record)
	{
		customerList.addItem({select:true, area:obj.areaCodes, extnsion_num:obj.extNumber, ipaddress:obj.IP});
	}
	customerDetailsGrid.dataProvider = customerList;
}

public var delListL:ArrayCollection;
protected function del_clickHandler(event:MouseEvent):void
{
	delListL = new ArrayCollection();
	
	for each (var obj:Object in customerDetailsGrid.dataProvider)
	{
		if(obj.select==true)
		{	
			delListL.addItemAt(obj.extnsion_num, 0);
			delListL.addItemAt(obj.ipaddress, 1);
		}
	}
	
	infoObject["date"] = new Date();			
	infoObject["extencode"] = delListL;
		
	deleteData.send(infoObject);
}

public function onDelData(event:ResultEvent):void
{
	customerXML = event.result as XML;
	if(customerXML.booleanvalue == "true")
	{
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
		infoObject["date"] = new Date();
		infoObject["areacode"] = areacode.selectedLabel;
		getData.send(infoObject);
		getExtensionNumber.send(infoObject);
	}
	else
	{
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
}